package com.edou.system.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.edou.auth.entity.User;
import com.edou.auth.service.IUserService;
import com.edou.base.page.PageResult;
import com.edou.system.dao.ISysLogDao;
import com.edou.system.dto.SysLogCondition;
import com.edou.system.entity.SysLog;
import com.edou.system.service.ISysLogService;

@Service("sysLogServiceImpl")
@Transactional(value = "transactionManager")
public class SysLogServiceImpl implements ISysLogService{

	@Resource(name = "sysLogDaoImpl")
	private ISysLogDao sysLogDao;
	
	@Resource(name = "userServiceImpl")
	private IUserService userService;
 
	/**
	 * 分页查询
	 * @param pageResult 分页对象
	 * @param condition 查询条件类
	 */
	public void listSysLogByPage(PageResult<SysLog> pageResult,SysLogCondition condition){
		int rows = sysLogDao.listSysLogByPageCount(condition);
		pageResult.setRows(rows);

		RowBounds rowBounds = new RowBounds(pageResult.getCurrentPageIndex(),pageResult.getPageSize());
		List<SysLog> list = sysLogDao.listSysLogByPage(rowBounds,condition);
		pageResult.setResult(list);
	}

	/**
	 * 根据ID查询
	 * @param id 主键
	 * @return 网站日志
	 */
	public SysLog getSysLogById(String id){
		return sysLogDao.getSysLogById(id);
	}

	/**
	 * 新增
	 * @param item 网站日志
	 */
	public void add(SysLog item){
		sysLogDao.add(item);
	}

	/**
	 * 修改
	 * @param item 网站日志
	 */
	public void update(SysLog item){
		sysLogDao.update(item);
	}

	/**
	 * 根据ID集合批量删除
	 * @param ids ID集合
	 */
	public void delByIds(String[] ids){
		sysLogDao.delByIds(ids);
	}
	/**
	 * 获得操作数据
	 */
	@Override
	public List<String> getCalcData() {
		return sysLogDao.getCalcData();
	}

	/**
	 * 添加网站日志
	 */
	public void add(String type, String desc, String createBy) {
		SysLog sysLog = new SysLog();
		sysLog.setType(type);
		sysLog.setCreateBy(createBy);
		sysLog.setDesc(desc);
		sysLogDao.add(sysLog);
	}

	/**
	 * 记录后台日志，操作人为当前后台登录用户
	 * @param type
	 * @param content
	 */
	public void log(String type, String content) {
		User user = userService.getCurrentUser();
		
		SysLog sysLog = new SysLog();
		sysLog.setType(type);
		sysLog.setCreateId(user.getId());
		sysLog.setCreateBy(user.getAccount());
		sysLog.setDesc(content);
		sysLogDao.add(sysLog);
	}

	
	
}

